package com.zt.fbg.autogenerator.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.util.StringUtils;

import java.util.Locale;
import java.util.ResourceBundle;


public class LanguageUtil {


    private final MessageSource messageSource;

    public LanguageUtil(MessageSource messageSource){
        this.messageSource = messageSource;
    }

    public  String getMessage(String msgKey,Object[] args,Locale locale){
        return this.messageSource.getMessage(msgKey,args,locale);
    }

    public  String getMessage(String msgKey,Locale locale){
        return this.messageSource.getMessage(msgKey,null,locale);
    }


    public static String getValueByLanguage(String language, String country, String key){
        String result= "";
        if(StringUtils.isEmpty(language) || StringUtils.isEmpty(country)){
            language = "zh";
            country = "CN";
        }


        Locale locale = null;
        try {
            locale = new Locale(language, country);
            ResourceBundle bundle = ResourceBundle.getBundle("static.i18n.messages.button", locale);
            result = bundle.getString(key);
        } catch (Exception exception) {
            exception.printStackTrace();
        }

        return result;
    }



}
